package com.sdry.service.impl.cb;


import com.sdry.mapper.cb.AnomalyRecordsMapper;
import com.sdry.model.cb.AnomalyRecordsEntity;
import com.sdry.model.cb.JsonUtil;
import com.sdry.model.cb.TypeEnum;
import com.sdry.service.cb.AnomalyRecordsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.sdry.utils.BaseUtils.getSession;

/**
 * @version: 1.0
 * @description: 异常记录
 * @author: cb
 * @create: 2019-11-27 11:11
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class AnomalyRecordsServiceImpl implements AnomalyRecordsService {

    @Autowired
    private AnomalyRecordsMapper anomalyRecordsMapper;



    @Override
    public List<AnomalyRecordsEntity> getAnomalyRecordsList(AnomalyRecordsEntity anomaly) {
        anomaly.setWareHouseId(getSession());
        return anomalyRecordsMapper.getAnomalyRecordsList(anomaly);
    }

    @Override
    public Integer getAnomalyRecordsTotal(AnomalyRecordsEntity anomaly) {
        anomaly.setWareHouseId(getSession());
        return anomalyRecordsMapper.getAnomalyRecordsTotal(anomaly);
    }

    @Override
    public JsonUtil addAnomalyRecords(AnomalyRecordsEntity anomaly) {
        Timestamp reviceDate = Timestamp.valueOf(anomaly.getAnomRemarks2());
        //收货时间
        anomaly.setReceiveDate(reviceDate);
        //创建时间
        anomaly.setCreateDate(new Date());
        //异常状态
        anomaly.setAnomState(TypeEnum.TYPE_STATE_WC.getName());
        Integer affected = anomalyRecordsMapper.addAnomalyRecords(anomaly);
        JsonUtil jsonUtil = new JsonUtil();
        if (affected > 0) {
            return jsonUtil;
        }
        jsonUtil.setFlag(false);
        return jsonUtil;
    }

    @Override
    public JsonUtil updateAnomalyRecords(AnomalyRecordsEntity anomaly) {
        Timestamp reviceDate = Timestamp.valueOf(anomaly.getAnomRemarks2());
        //收货时间
        anomaly.setReceiveDate(reviceDate);
        //修改时间
        anomaly.setUpdateDate(new Date());
        //异常状态
        anomaly.setAnomState(TypeEnum.TYPE_STATE_WC.getName());
        Integer affected = anomalyRecordsMapper.updateAnomalyRecords(anomaly);
        JsonUtil jsonUtil = new JsonUtil();
        if (affected > 0) {
            return jsonUtil;
        }
        jsonUtil.setFlag(false);
        return jsonUtil;

    }

    @Override
    public JsonUtil deleteAnomalyRecords(String[] ids, String type) {
        Integer affected = 0;
        if (StringUtils.equals("delete",type)) {
            for (String id : ids) {
                affected = anomalyRecordsMapper.deleteAnomalyRecords(Long.parseLong(id));
            }
        }else {
            for (String id : ids) {
                affected = anomalyRecordsMapper.deleteAnomalyRecordsState(Long.parseLong(id),TypeEnum.TYPE_STATE_YC.getName());
            }
        }

        JsonUtil jsonUtil = new JsonUtil();
        if (affected > 0) {
            return jsonUtil;
        }
        jsonUtil.setFlag(false);
        return jsonUtil;
    }

    @Override
    public List<String> getReceiveNumber() {
        return anomalyRecordsMapper.getReceiveNumber(getSession());
    }


}
